package com.situ.staff.employee.service.Impl;

import java.sql.SQLException;
import java.util.List;

import com.situ.staff.employee.dao.EmployeeDao;
import com.situ.staff.employee.model.EmployeeModel;
import com.situ.staff.employee.service.IEmployeeService;

public class EmployeeServiceImpl implements IEmployeeService {

	private EmployeeDao dao = new EmployeeDao();
	public static String pass = "123456" ;
	@Override
	public String insert(EmployeeModel model) throws SQLException {
		EmployeeModel m0 = new EmployeeModel(model.getCode());
		EmployeeModel m1 = dao.selectModel(m0);
		if(model.getPasswrod()==""||model.getPasswrod()==null) {
			model.setPasswrod(pass);
		}
		if (m1 == null) {
			Integer res = dao.insert(model);
			return res == null ? null : res.toString();
		} else {
			return "repeat";
		}
	}

	@Override
	public String delete(EmployeeModel model) throws SQLException {
		
		
		return dao.delete(model) + "";
	}

	@Override
	public String update(EmployeeModel model) throws SQLException {
		model.setPasswrod(pass);
		return dao.updateActive(model) + "";
	}

	@Override
	public List<EmployeeModel> selectList(EmployeeModel model) throws SQLException {
		String code = model.getCode();
		//给code属性赋值，这里的值带有通配符，是为了进行模糊查询
		model.setCode(code == null ? "%%" : "%" + code + "%");
		String name = model.getName();
		model.setName(name == null ? "%%" : "%" + name + "%");
		return dao.selectList(model);
	}

	@Override
	public EmployeeModel selectModel(EmployeeModel model) throws SQLException {
		EmployeeModel m0 = new EmployeeModel(model.getCode());
//		System.out.println(m0.toString());
		return dao.selectModel(m0);
	}

	public String login(EmployeeModel model) throws SQLException {
		EmployeeModel m1 = dao.selectModel(new EmployeeModel(model.getCode()));
//		System.out.println(model.toString());
//				System.out.println(m1.toString());
		if (m1 == null) {
			return "unReg";
		} else if (!m1.getPasswrod().equals(model.getPasswrod())) {
			return "passerror";
		} else {
			return "success";
		}
	}

	public Integer selectCount(EmployeeModel model) {
		EmployeeModel m0 = new EmployeeModel();
		String code = model.getCode();
		//给对象的属性赋值，利用这个值进行模糊查询，查询出符合模糊条件的记录条数
		m0.setCode(code == null ? "%%" : "%" + code + "%");
		String name = model.getName();
		m0.setName(name == null ? "%%" : "%" + name + "%");
		return dao.selectCount(m0);
	}

	public String updateDept(EmployeeModel model) throws SQLException {
		
		return dao.updateActive(model)+"";
	}
	
}
